package cn.iocoder.yudao.module.member.controller.admin.useranswerremark;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.useranswerremark.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.useranswerremark.UserAnswerRemarkDO;
import cn.iocoder.yudao.module.member.service.useranswerremark.UserAnswerRemarkService;

@Tag(name = "管理后台 - 用户问答评论")
@RestController
@RequestMapping("/member/user-answer-remark")
@Validated
public class UserAnswerRemarkController {

    @Resource
    private UserAnswerRemarkService userAnswerRemarkService;

    @PostMapping("/create")
    @Operation(summary = "创建用户问答评论")
    @PreAuthorize("@ss.hasPermission('member:user-answer-remark:create')")
    public CommonResult<Long> createUserAnswerRemark(@Valid @RequestBody UserAnswerRemarkSaveReqVO createReqVO) {
        return success(userAnswerRemarkService.createUserAnswerRemark(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户问答评论")
    @PreAuthorize("@ss.hasPermission('member:user-answer-remark:update')")
    public CommonResult<Boolean> updateUserAnswerRemark(@Valid @RequestBody UserAnswerRemarkSaveReqVO updateReqVO) {
        userAnswerRemarkService.updateUserAnswerRemark(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户问答评论")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-answer-remark:delete')")
    public CommonResult<Boolean> deleteUserAnswerRemark(@RequestParam("id") Long id) {
        userAnswerRemarkService.deleteUserAnswerRemark(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户问答评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-answer-remark:query')")
    public CommonResult<UserAnswerRemarkRespVO> getUserAnswerRemark(@RequestParam("id") Long id) {
        UserAnswerRemarkDO userAnswerRemark = userAnswerRemarkService.getUserAnswerRemark(id);
        return success(BeanUtils.toBean(userAnswerRemark, UserAnswerRemarkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户问答评论分页")
    @PreAuthorize("@ss.hasPermission('member:user-answer-remark:query')")
    public CommonResult<PageResult<UserAnswerRemarkRespVO>> getUserAnswerRemarkPage(@Valid UserAnswerRemarkPageReqVO pageReqVO) {
        PageResult<UserAnswerRemarkDO> pageResult = userAnswerRemarkService.getUserAnswerRemarkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserAnswerRemarkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户问答评论 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-answer-remark:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserAnswerRemarkExcel(@Valid UserAnswerRemarkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserAnswerRemarkDO> list = userAnswerRemarkService.getUserAnswerRemarkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户问答评论.xls", "数据", UserAnswerRemarkRespVO.class,
                        BeanUtils.toBean(list, UserAnswerRemarkRespVO.class));
    }

}